package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://epaper.my0538.com/hgzplpmrdx/taianribao/content/20121204/Page01IP.htm 泰安日报
 * http://epaper.my0538.com/hgzplpmrdx/tswb/content/20121204/PageA01YF.htm  泰山晚报
 * http://epaper.my0538.com/hgzplpmrdx/CXZF/content/20121127/PageNJ01TB.htm 城乡致富周刊
 * 
 * @author allen 
 */
public class My0538NewsPaperAnalyse implements AnalyseNews {
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.my0538.com/hgzplpmrdx/taianribao/content/[0-9]{8}/Articel.*.htm",
				"http://epaper.my0538.com/hgzplpmrdx/tswb/content/[0-9]{8}/Articel.*.htm",
				"http://epaper.my0538.com/hgzplpmrdx/CXZF/content/[0-9]{8}/Articel.*.htm",
				"http://222.132.157.151:9988/.*/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9].*.htm"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();	
		
				
		newspaper.setUrl(url);		
		
		
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
	    
		title =doc.select("div.detailtitle").text();
		if(title==null || title.trim().length()==0){
			title =doc.select("html body table tbody tr td table tbody tr td strong").text();
			
		}		
        
		date=DateUtils.matchDate(url.replace("ttp://222.132.157.151:9988/", ""));
		
		content =HtmlCleaner.getContentHtml(url,doc.select("html body div table tbody tr td table tbody tr td table tbody tr td table"));
		if(content==null || content.trim().length()==0){
			content =HtmlCleaner.getContentHtml(url,doc.select("html body table tbody tr td table tbody tr td.px12c table tbody tr td"));
		}
		
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.my0538.com/hgzplpmrdx/taianribao/content/[0-9]{8}/Page.*.htm",
				"http://epaper.my0538.com/hgzplpmrdx/tswb/content/[0-9]{8}/Page.*.htm",
				"http://epaper.my0538.com/hgzplpmrdx/CXZF/content/[0-9]{8}/Page.*.htm",
				"http://222.132.157.151:9988/.*/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	

	
	
}
